// pages/findTeam/index.js
const util = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    teamlist:[],
    contestTeams:[],
    houseTeams:[],
    playTeams:[],
    otherTeams:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTeamList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getTeamList: function(){
    this.setData({
      openid: wx.getStorageSync('OPENID')
    })
    wx.cloud.database().collection('teamlist').get().then((res) => {
      console.log(res.data)
      
      this.setData({
        teamlist: res.data.reverse(),
      })
      this.divideTeams(this.data.teamlist)

    })
  },
  divideTeams:function(teamlist){
    let contestTeams = teamlist.filter(item=>{
      return item.teamtype=='一起比赛'&&item.teamimg!==''
    }).slice(0,3)
    let houseTeams = teamlist.filter(item=>{
      return item.teamtype=='一起租房'&&item.teamimg!==''
    }).slice(0,3)
    let playTeams = teamlist.filter(item=>{
      return item.teamtype=='一起出去玩'&&item.teamimg!==''
    }).slice(0,3)
    let otherTeams = teamlist.filter(item=>{
      return item.teamtype=='其他'&&item.teamimg!==''
    }).slice(0,3)
    console.log(teamlist)
    this.setData({
      contestTeams:contestTeams,
      houseTeams:houseTeams,
      playTeams:playTeams,
      otherTeams:otherTeams
    })
  },
  addteam: function (e) {
    let teamid = e.currentTarget.dataset.teamid
    let openid = e.currentTarget.dataset.openid
    let member = e.currentTarget.dataset.member,meopenid=wx.getStorageSync('OPENID')
    if (this.data.openid === openid) {
      wx.showToast({
        title: '不能加入自己发布的组队！',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    let ismember=false 
    console.log(member)
    member.forEach(item=>{
      if(item.openid==meopenid){
        ismember = true
      }
    })
    if (ismember) {
      wx.showToast({
        title: '不能重复加入一个队伍！',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '确定申请吗？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: (result) => {
        if (result.confirm) {
          wx.navigateTo({
            url: `/pages/application/application?teamid=${teamid}`,
          })

        }
      },
      fail: () => {
        return false
      },
      complete: () => {},
    })
  },
  gotopublish: function(){
    wx.navigateTo({
      url: '/pages/teamForm/index',
    })
  },
  gotofind: function(e){
    let type = e.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/findTeam/index?type='+type,
    })
  },
  goaboutTeam:function(){
    wx.navigateTo({
      url: '/pages/teamaboutMe/index',
    })
  },
  onPullDownRefresh: function () {
    this.getTeamList()
    wx.stopPullDownRefresh()
  },
  goteamDetail:function (e) {
    let id = e.currentTarget.dataset.teamid
    wx.navigateTo({
      url: `/pages/teamDetail/index?id=${id}`,
    })
  },
})